﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DragItemUI : MonoBehaviour, IDragHandler, IEndDragHandler,IBeginDragHandler
{
    public Vector3 offset;

    private RaycastHit hit;

    private GameObject obj;

    public float scaleCoe = 10.0f;

    public RectTransform selfRect;

    public Image image;

    private Transform camTran;

    private float z_middle = 0.0f;

    private SpriteRenderer obj_Image;

    private RectTransform m_RT;

    private bool isUse = false;

    void Start()
    {
        obj = Instantiate(Resources.Load<GameObject>("Circle"));

        obj.name = "准星";

        obj_Image = obj.GetComponent<SpriteRenderer>();

        obj.transform.SetParent(MeasurementManager.Instance.transform);

        camTran = Camera.main.transform;

        m_RT = transform.GetComponent<RectTransform>();

        obj.SetActive(false);
    }

    public void SetScale(float scale)
    {
        obj.transform.localScale = Vector3.one * scale;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        selfRect.localScale = Vector3.one * 1.2f;

        image.color = Color.blue;
    }

    public void OnDrag(PointerEventData eventData)
    {
        /*Global.MoveCamera = false; //锁死摄像机移动*/

        obj.SetActive(true);

        m_RT.position += new Vector3(eventData.delta.x, eventData.delta.y, 0);

        m_RT.anchoredPosition = new Vector2(Mathf.Clamp(m_RT.anchoredPosition.x, -Screen.width / 2.0f, Screen.width / 2.0f), Mathf.Clamp(m_RT.anchoredPosition.y, -Screen.height / 2.0f, Screen.height / 2.0f));

        Ray ray = Camera.main.ScreenPointToRay(m_RT.position + offset);

        if (Physics.Raycast(ray, out hit, 1000))
        {
            MeshCollider meshCollider = hit.collider as MeshCollider;

            if (meshCollider != null && meshCollider.sharedMesh != null)
            {
                isUse = true;

                obj.transform.localPosition = hit.point;

                z_middle = -hit.point.z;

                obj.transform.localScale = Vector3.one * (Vector3.Distance(hit.point, camTran.position) * MeasurementManager.Instance.pointScaleCoe);

                MeasurementManager.Instance.HitObj = hit.collider.gameObject;

                if (MeasurementManager.Instance.IsVertices(hit.point).Item1)
                {
                    obj_Image.color = Color.green;

                    obj_Image.sprite = MeasurementManager.Instance.pointSprite;

                    obj.transform.position = MeasurementManager.Instance.IsVertices(hit.point).Item2;

                    obj.transform.localScale *= 0.5f;
                }
                else
                {
                    obj_Image.color = Color.blue;

                    obj_Image.sprite = MeasurementManager.Instance.verticeSprite;

                    obj.transform.forward = hit.normal;
                }

                MeasurementManager.Instance.ChangingOnePoint(obj.transform.position);
            }
        }
        else
        {
            isUse = false;

            FakeMove();
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        selfRect.localScale = Vector3.one;

        image.color = Color.white;

        /*解放摄像机移动权限 Global.MoveCamera = true;*/

        obj.SetActive(false);

        if (!isUse)
        {
            return;
        }

        MeasurementManager.Instance.EnsureOnePoint(obj.transform.localPosition, obj.transform.forward);
    }

    private void FakeMove()
    {
        obj_Image.color = Color.white;

        obj_Image.sprite = MeasurementManager.Instance.verticeSprite;

        obj.transform.position = Camera.main.ScreenToWorldPoint(m_RT.position + offset + new Vector3(0, 0, z_middle));
    }

    public void OnDestroy()
    {
        Destroy(obj);
    }
}
